package com.paobuji.aidebao.myapplication.ui;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.jaeger.library.StatusBarUtil;
import com.paobuji.aidebao.myapplication.R;
import com.paobuji.aidebao.myapplication.activity.BaseActivity;
import com.paobuji.aidebao.myapplication.adapter.MyRankingListAdapter;
import com.paobuji.aidebao.myapplication.appinterface.GetInterfaceObjctListener;
import com.paobuji.aidebao.myapplication.bean.GetRankingList;
import com.paobuji.aidebao.myapplication.bean.UserBeanResult;
import com.paobuji.aidebao.myapplication.tool.ACache;
import com.paobuji.aidebao.myapplication.tool.MyIntentTools;
import com.paobuji.aidebao.myapplication.view.CircleImageView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MyRankingActivity extends BaseActivity {

    @BindView(R.id.myUserImage)
    CircleImageView myUserImage;
    @BindView(R.id.userNameText)
    TextView userNameText;
    @BindView(R.id.userNum)
    TextView userNum;
    @BindView(R.id.userDis)
    TextView userDis;
    @BindView(R.id.userBackImg)
    ImageView userBackImg;
    private List<GetRankingList.DataBean.CollectionlistBean> list;
    Context context;
    RecyclerView recyclerView;
    MyRankingListAdapter adapter;
    Toolbar toolbar;
    UserBeanResult.DataBean.UserBean userBean;
    ACache aCache;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_my_ranking);
        ButterKnife.bind(this);
        //设置全透明
        StatusBarUtil.setTransparent(this);

        context = getApplicationContext();
        aCache = ACache.get(context);
        userBean = (UserBeanResult.DataBean.UserBean) aCache.getAsObject("userBean");
        if (userBean == null) {

        } else {
            changeView();
        }

        toolbar = (Toolbar) findViewById(R.id.tool_bar);
        toolbar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        getData(0);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {

    }

    //跟新界面信息
    public void changeView() {
        Glide.with(context)
                // .load(mResultsPath.get(0))
                .load(userBean.getUserImg())
                .asBitmap()
                .skipMemoryCache(false) //跳过内存缓存
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)     //缓存设置
                .priority(Priority.NORMAL)    //优先级  LOW     NORMAL      HIGH      IMMEDIATE
                .placeholder(R.mipmap.zhanwei)    //占位图
                .error(R.mipmap.zhanwei)
                .into(userBackImg);
        Glide.with(context)
                // .load(mResultsPath.get(0))
                .load(userBean.getUserImg())
                .asBitmap()
                .skipMemoryCache(false) //跳过内存缓存
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)     //缓存设置
                .priority(Priority.NORMAL)    //优先级  LOW     NORMAL      HIGH      IMMEDIATE
                .placeholder(R.mipmap.zhanwei)    //占位图
                .error(R.mipmap.zhanwei)
                .into(myUserImage);
        userNameText.setText(userBean.getUsername());
        userNum.setText("999");
        userDis.setText(userBean.getAllDis() + "");
    }

    //从网络获取世界排名集合
    public void getData(int n) {
        MyIntentTools myIntentTools = new MyIntentTools();
        myIntentTools.getRankingList(context, n, 25);
        myIntentTools.setGetInterfaceObjctListener(new GetInterfaceObjctListener() {
            @Override
            public void getObjectDate(Object data) {
                if (((GetRankingList) data).getCode() == 0) {                      //拿到数据
                    list = ((GetRankingList) data).getData().getCollectionlist();
                    adapter = new MyRankingListAdapter(context, list);
                    recyclerView.setAdapter(adapter);
                } else {
                    toastLog(R.string.erroneous + "");
                }
            }
        });
    }
}
